home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: usinternet.com!not-for-mail
- From: Scott Jibben <sjibben@usinternet.com>
- Subject: Re: a question on "return"
- Message-ID: <3116320B.41A2@usinternet.com>
- Date: Mon, 05 Feb 1996 10:36:27 -0600
- Organization: Jibben Software
- X-Mailer: Mozilla 2.0b6a (WinNT; I)
- MIME-Version: 1.0
- References: <4f2ipq$kaf@srvr1.engin.umich.edu>
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
-
- Chih-Jen Lin wrote:
- >
- > In C, usually we treat 1 as true and 0 as false.
- > However, we usually use return(0) after normal end of an int
- > function and return(1) when some errors happen.
- > Can some one tell me why we return a false value after normal
- > end of a function ?
- >
- > Thanks in advance.
- >
- > Chih-Jen Lin
-
- Although I've never heard of a real reason. Here's why I write
- C software using 0 (false) for success. Because, you generally
- can only succeed in one way. There may be several ways a
- function may fail. Since there is only one value for false
- (zero) and many values that are true (non-zero). It is easier
- to let the calling software know what failed by returning a
- non-zero value.
-
- If I had written the functions so that a true value meant
- success, then I'd have to find another way to let the calling
- function know how the function failed (i.e. a parameter that is
- a pointer to an int, global variable (yech), etc.).
-
- Also, although a value of one is considered true. I use -1 (~0)
- to represent true.
-
- May I suggest a book? Get a copy of "Writing Solid Code"
- published by Microsoft Press. It gives many helpful suggestions
- to writing more stable/error detecting code.
-
- --
- Scott Jibben, Jibben Software
- Galactic Overlord & Mines of Gorr BBS Door Games
- -----------------------------
- [EMAIL] sjibben@usinternet.com
- [WWW] http://www.usinternet.com/jsw
- [FTP] ftp.europa.com /outgoing/DOORS/jibben
- [FTP] ftp.cts.com /pub/dferber/jibben
- [FIDO] 1:282/115 [BBS] 612-379-8272 (10 USR 28.8
- Couriers)
- [VOICE] 612-757-5626 [FAX] 612-757-8687
-
-